<?php
session_start();
/*
***********************************
CopyRight 2011 -Scout4all
***********************************
*/

include"./../functions.php";
include"./../$config";
if(isset($_SESSION['username'])){ 
$db_u_id=$_SESSION['u_id'];
$user_s_name=$_SESSION['username'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INDEX</title>

<link rel="stylesheet" href="css/menu_style.css" type="text/css" />
</head>

<body>
<div id='header'>
<ul>
<?php 
echo"<li>".$user_s_name." مرحبا بك </li>";
?>
<li><a href='logout.php'>تسجيل الخروج</a></li>
</ul>
</div>
<div id="menucontainer">
	<div id="menunav">
		<ul>
			<li><a href="index.php" title="css website navigation menus"><span>الرئيسيه</span></a></li>
			<li><a href="users.php" title="css website navigation menus"  class="current"><span>الاعضاء</span></a></li>
			<li><a href="artical.php" title="css website navigation menus"><span>المقالات</span></a></li>

		</ul>
	</div>
</div>
<div id="container">
  <div class="right">
  <ul>
   <li><a href='users.php'  class="current">قائمه الاعضاء</a></li>
  <li><a href='add_users.php'>اضافه اعضاء</a></li>

  </ul> 
  </div>
  <div class="left">
<?php

$gid=intval($_GET['id']);
$id=$_POST['id'];
$username=$_POST['username'];
$password=md5($_POST['password']);

if(isset($_POST['edit']) && $_POST['edit']=='user'){
$sql="UPDATE users
SET username='$username', password='$password'
WHERE u_id='$id' ";
$query=mysql_query($sql) or die("@mysql_error");
if($query){
echo"Done";
}

}

$query=mysql_query("select * from users where u_id='$gid'") or die(err);
$res=mysql_fetch_assoc($query);
$res_u=$res['username'];

if($_REQUEST['do'] == 'edit'){
echo"
<form action='".$_SERVER['PHP_SELF']."' method='post'>
<table>
<tr><td>username :</td><td> <input type='text' name='username' value='$res_u'></td><tr>
<tr><td>password : </td><td><input type='password' name='password'></td><tr>
<tr><td>conf pass :</td><td><input type='password' name='confpass'></td><tr>
<tr><td><input type='submit' value='save'>
<input type='hidden' name='id' value='$gid'>
<input type='hidden' name='edit' value='user'>
</td><tr>
</table>
</form>";

}
/// حذف عضو_

$id=$_POST['id'];
$delete=$_POST['delete'];
if($delete){
 if(!empty($id)){
    $imid=implode(",",$id);
  //echo"$imid";
  $sql="DELETE FROM users WHERE u_id IN($imid)" OR DIE('USER/S NOT DELETED');
  $query=mysql_query($sql);
  if(!empty($query)){
  echo "<h4>تم حذف الاعضاء من قاعدة البيانات </h4>";
  }
  else{
  echo"تم حذف هذا العضو مسبقا";
  }
 }
 else{
 echo"<h4>الرجاء اختيار عضو لحذفه </h4>";

 }
}
if(!isset($_GET['pag'])){
$pag = 1;
} else {
$pag = $_GET['pag'];
}
$max_results = 1;
$start = (($pag * $max_results) - $max_results);
//////////// استعلامات
$data=mysql_query("select * from users order by u_id asc  LIMIT $start, $max_results ");
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>
<table border='1px' width='500px'>
<tr>
<td>العضو</td>
<td>عمليات
<br />تعديل || حذف
</td>
</tr>

";
while($result=mysql_fetch_assoc($data)){
$user_id=$result['u_id'];
$username=$result['username'];
echo"
<tr><td>$username</td>
<td><a href='users.php?do=edit&id=$user_id'>تعديل</a> ||<input type='checkbox' name='id[]' value='$user_id'> </td>
</tr>
";
//<a href='user_edit.php?user_id=$user_id'>حذف</a>
}
mysql_free_result($query);
echo"
<tr><td></td><td><input type='submit' name='delete' value='delete'><input type='reset' name='reset' value='مسح المحدد'>
</table>";
$total_res = mysql_result(mysql_query("SELECT COUNT(u_id) FROM users"),0);
$total_col = ceil($total_res / $max_results);
if($pag > 1){
$prev = ($pag - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?pag=$prev\">&nbsp;السابق&nbsp;</a>&nbsp;";
}
for($i = 1; $i <= $total_col; $i++){
if(($pag) == $i){
echo "$i&nbsp;";
}
else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pag=$i\">$i</a>&nbsp;";
}
}
if($pag < $total_col){
$next = ($pag + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?pag=$next\" >&nbsp;التالي&nbsp;</a>";
}
mysql_close($connection);


?>
  </div>
</div>
</body>
</html>
<?php
}
else{
header("location: login.php");
}
?>